package com.damon.android.interview;

/**
 * Created by Damon.Han on 2018/10/11 0011.
 *
 * @author Damon
 */

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.damon.android.interview.binder.BinderFragment;
import com.damon.android.interview.service.ServiceFragment;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */

    private static final String ARG_SECTION_NUMBER = "section_number";
    private FrameLayout frameLayout;

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        int num = getArguments().getInt(ARG_SECTION_NUMBER);
        textView.setText(getString(R.string.section_format, num));
        frameLayout = rootView.findViewById(R.id.content_area);
        initContentArea(num);
        return rootView;
    }

    private void initContentArea(int num) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (num) {
            case 0:
                fragmentTransaction.replace(R.id.content_area, ServiceFragment.newInstance());
                break;
            case 1:
                fragmentTransaction.replace(R.id.content_area, BinderFragment.newInstance());
                break;
            case 2:
                break;
        }

        fragmentTransaction.commit();
    }
}